package com.example.tliaswebmanagement.service.impl;

import com.example.tliaswebmanagement.entity.Clazz;
import com.example.tliaswebmanagement.entity.PageResult;
import com.example.tliaswebmanagement.mapper.ClazzMapper;
import com.example.tliaswebmanagement.service.ClazzService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    private final ClazzMapper clazzMapper;

    @Autowired
    public ClazzServiceImpl(ClazzMapper clazzMapper) {
        this.clazzMapper = clazzMapper;
    }

    @Override
    public PageResult findAll(Integer page,Integer pageSize, String name, LocalDate begin, LocalDate end) {
        PageHelper.startPage(page, pageSize);
        List<Clazz> clazzes=clazzMapper.findAll(name, begin, end);
        Page<Clazz> p = (Page<Clazz>) clazzes;
        return new PageResult(p.getTotal(), p.getResult());
    }

    @Transactional
    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void insert(Clazz clazz) {
        //先设置创建时间和更新时间
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        //插入表中
        clazzMapper.insert(clazz);
    }

    @Override
    public Clazz findById(Integer id) {
        return clazzMapper.findById(id);
    }

    @Transactional
    @Override
    public void update(Clazz clazz) {
        //先设置更新时间
        clazz.setUpdateTime(LocalDateTime.now());
        //更新表中
        clazzMapper.update(clazz);
    }

    @Override
    public List<Clazz> getAll() {
        return clazzMapper.getAll();
    }
}
